import {
  Body,
  ClassSerializerInterceptor,
  Controller,
  Post,
  UseFilters,
  UseInterceptors,
  UseGuards,
  Req,
} from "@nestjs/common";
import { AuthService } from "./auth.service";
import { SigninUserDto } from "./dto/signin-user.dto";
import { SignupUserDto } from "./dto/signup-user.dto";
import { JwtGuard } from "../guards/jwt.guard";

@Controller("auth")
@UseInterceptors(ClassSerializerInterceptor)
export class AuthController {
  constructor(private authService: AuthService) {}

  @Post("signin")
  async signin(@Body() dto: SigninUserDto) {
    const { usernameOrEmail, password } = dto;
    const token = await this.authService.signin(password, usernameOrEmail);
    return {
      access_token: token,
    };
  }

  @Post("signup")
  async signup(@Body() dto: SignupUserDto) {
    const { username, password, email } = dto;
    return await this.authService.singup(username, password, email);
  }

  @Post("signout")
  @UseGuards(JwtGuard)
  async signout(@Req() req: any) {
    const userId = req.user.userId;
    return await this.authService.signout(userId);
  }
}
